<?php


/**
 * Base class that represents a row from the 'provinces' table.
 *
 *
 *
 * @package    propel.generator.nawragon.om
 */
abstract class BaseProvinces extends BaseObject implements Persistent
{
    /**
     * Peer class name
     */
    const PEER = 'ProvincesPeer';

    /**
     * The Peer class.
     * Instance provides a convenient way of calling static methods on a class
     * that calling code may not be able to identify.
     * @var        ProvincesPeer
     */
    protected static $peer;

    /**
     * The flag var to prevent infinit loop in deep copy
     * @var       boolean
     */
    protected $startCopy = false;

    /**
     * The value for the id field.
     * @var        int
     */
    protected $id;

    /**
     * The value for the game_id field.
     * @var        int
     */
    protected $game_id;

    /**
     * The value for the player_id field.
     * @var        int
     */
    protected $player_id;

    /**
     * The value for the province_def_id field.
     * @var        int
     */
    protected $province_def_id;

    /**
     * The value for the active field.
     * Note: this column has a database default value of: 1
     * @var        int
     */
    protected $active;

    /**
     * @var        ProvinceDefs
     */
    protected $aProvinceDefs;

    /**
     * @var        Players
     */
    protected $aPlayers;

    /**
     * @var        PropelObjectCollection|Army[] Collection to store aggregation of Army objects.
     */
    protected $collArmys;
    protected $collArmysPartial;

    /**
     * @var        PropelObjectCollection|Infrastructures[] Collection to store aggregation of Infrastructures objects.
     */
    protected $collInfrastructuress;
    protected $collInfrastructuressPartial;

    /**
     * @var        PropelObjectCollection|Materials[] Collection to store aggregation of Materials objects.
     */
    protected $collMaterialss;
    protected $collMaterialssPartial;

    /**
     * @var        PropelObjectCollection|ProvinceHistories[] Collection to store aggregation of ProvinceHistories objects.
     */
    protected $collProvinceHistoriess;
    protected $collProvinceHistoriessPartial;

    /**
     * @var        PropelObjectCollection|War[] Collection to store aggregation of War objects.
     */
    protected $collWarsRelatedByAttackerProvinceId;
    protected $collWarsRelatedByAttackerProvinceIdPartial;

    /**
     * @var        PropelObjectCollection|War[] Collection to store aggregation of War objects.
     */
    protected $collWarsRelatedByOpponentProvinceId;
    protected $collWarsRelatedByOpponentProvinceIdPartial;

    /**
     * Flag to prevent endless save loop, if this object is referenced
     * by another object which falls in this transaction.
     * @var        boolean
     */
    protected $alreadyInSave = false;

    /**
     * Flag to prevent endless validation loop, if this object is referenced
     * by another object which falls in this transaction.
     * @var        boolean
     */
    protected $alreadyInValidation = false;

    /**
     * Flag to prevent endless clearAllReferences($deep=true) loop, if this object is referenced
     * @var        boolean
     */
    protected $alreadyInClearAllReferencesDeep = false;

    /**
     * An array of objects scheduled for deletion.
     * @var		PropelObjectCollection
     */
    protected $armysScheduledForDeletion = null;

    /**
     * An array of objects scheduled for deletion.
     * @var		PropelObjectCollection
     */
    protected $infrastructuressScheduledForDeletion = null;

    /**
     * An array of objects scheduled for deletion.
     * @var		PropelObjectCollection
     */
    protected $materialssScheduledForDeletion = null;

    /**
     * An array of objects scheduled for deletion.
     * @var		PropelObjectCollection
     */
    protected $provinceHistoriessScheduledForDeletion = null;

    /**
     * An array of objects scheduled for deletion.
     * @var		PropelObjectCollection
     */
    protected $warsRelatedByAttackerProvinceIdScheduledForDeletion = null;

    /**
     * An array of objects scheduled for deletion.
     * @var		PropelObjectCollection
     */
    protected $warsRelatedByOpponentProvinceIdScheduledForDeletion = null;

    /**
     * Applies default values to this object.
     * This method should be called from the object's constructor (or
     * equivalent initialization method).
     * @see        __construct()
     */
    public function applyDefaultValues()
    {
        $this->active = 1;
    }

    /**
     * Initializes internal state of BaseProvinces object.
     * @see        applyDefaults()
     */
    public function __construct()
    {
        parent::__construct();
        $this->applyDefaultValues();
    }

    /**
     * Get the [id] column value.
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Get the [game_id] column value.
     *
     * @return int
     */
    public function getGameId()
    {
        return $this->game_id;
    }

    /**
     * Get the [player_id] column value.
     *
     * @return int
     */
    public function getPlayerId()
    {
        return $this->player_id;
    }

    /**
     * Get the [province_def_id] column value.
     *
     * @return int
     */
    public function getProvinceDefId()
    {
        return $this->province_def_id;
    }

    /**
     * Get the [active] column value.
     *
     * @return int
     */
    public function getActive()
    {
        return $this->active;
    }

    /**
     * Set the value of [id] column.
     *
     * @param int $v new value
     * @return Provinces The current object (for fluent API support)
     */
    public function setId($v)
    {
        if ($v !== null && is_numeric($v)) {
            $v = (int) $v;
        }

        if ($this->id !== $v) {
            $this->id = $v;
            $this->modifiedColumns[] = ProvincesPeer::ID;
        }


        return $this;
    } // setId()

    /**
     * Set the value of [game_id] column.
     *
     * @param int $v new value
     * @return Provinces The current object (for fluent API support)
     */
    public function setGameId($v)
    {
        if ($v !== null && is_numeric($v)) {
            $v = (int) $v;
        }

        if ($this->game_id !== $v) {
            $this->game_id = $v;
            $this->modifiedColumns[] = ProvincesPeer::GAME_ID;
        }

        if ($this->aPlayers !== null && $this->aPlayers->getGameId() !== $v) {
            $this->aPlayers = null;
        }


        return $this;
    } // setGameId()

    /**
     * Set the value of [player_id] column.
     *
     * @param int $v new value
     * @return Provinces The current object (for fluent API support)
     */
    public function setPlayerId($v)
    {
        if ($v !== null && is_numeric($v)) {
            $v = (int) $v;
        }

        if ($this->player_id !== $v) {
            $this->player_id = $v;
            $this->modifiedColumns[] = ProvincesPeer::PLAYER_ID;
        }

        if ($this->aPlayers !== null && $this->aPlayers->getId() !== $v) {
            $this->aPlayers = null;
        }


        return $this;
    } // setPlayerId()

    /**
     * Set the value of [province_def_id] column.
     *
     * @param int $v new value
     * @return Provinces The current object (for fluent API support)
     */
    public function setProvinceDefId($v)
    {
        if ($v !== null && is_numeric($v)) {
            $v = (int) $v;
        }

        if ($this->province_def_id !== $v) {
            $this->province_def_id = $v;
            $this->modifiedColumns[] = ProvincesPeer::PROVINCE_DEF_ID;
        }

        if ($this->aProvinceDefs !== null && $this->aProvinceDefs->getId() !== $v) {
            $this->aProvinceDefs = null;
        }


        return $this;
    } // setProvinceDefId()

    /**
     * Set the value of [active] column.
     *
     * @param int $v new value
     * @return Provinces The current object (for fluent API support)
     */
    public function setActive($v)
    {
        if ($v !== null && is_numeric($v)) {
            $v = (int) $v;
        }

        if ($this->active !== $v) {
            $this->active = $v;
            $this->modifiedColumns[] = ProvincesPeer::ACTIVE;
        }


        return $this;
    } // setActive()

    /**
     * Indicates whether the columns in this object are only set to default values.
     *
     * This method can be used in conjunction with isModified() to indicate whether an object is both
     * modified _and_ has some values set which are non-default.
     *
     * @return boolean Whether the columns in this object are only been set with default values.
     */
    public function hasOnlyDefaultValues()
    {
            if ($this->active !== 1) {
                return false;
            }

        // otherwise, everything was equal, so return true
        return true;
    } // hasOnlyDefaultValues()

    /**
     * Hydrates (populates) the object variables with values from the database resultset.
     *
     * An offset (0-based "start column") is specified so that objects can be hydrated
     * with a subset of the columns in the resultset rows.  This is needed, for example,
     * for results of JOIN queries where the resultset row includes columns from two or
     * more tables.
     *
     * @param array $row The row returned by PDOStatement->fetch(PDO::FETCH_NUM)
     * @param int $startcol 0-based offset column which indicates which restultset column to start with.
     * @param boolean $rehydrate Whether this object is being re-hydrated from the database.
     * @return int             next starting column
     * @throws PropelException - Any caught Exception will be rewrapped as a PropelException.
     */
    public function hydrate($row, $startcol = 0, $rehydrate = false)
    {
        try {

            $this->id = ($row[$startcol + 0] !== null) ? (int) $row[$startcol + 0] : null;
            $this->game_id = ($row[$startcol + 1] !== null) ? (int) $row[$startcol + 1] : null;
            $this->player_id = ($row[$startcol + 2] !== null) ? (int) $row[$startcol + 2] : null;
            $this->province_def_id = ($row[$startcol + 3] !== null) ? (int) $row[$startcol + 3] : null;
            $this->active = ($row[$startcol + 4] !== null) ? (int) $row[$startcol + 4] : null;
            $this->resetModified();

            $this->setNew(false);

            if ($rehydrate) {
                $this->ensureConsistency();
            }
            $this->postHydrate($row, $startcol, $rehydrate);
            return $startcol + 5; // 5 = ProvincesPeer::NUM_HYDRATE_COLUMNS.

        } catch (Exception $e) {
            throw new PropelException("Error populating Provinces object", $e);
        }
    }

    /**
     * Checks and repairs the internal consistency of the object.
     *
     * This method is executed after an already-instantiated object is re-hydrated
     * from the database.  It exists to check any foreign keys to make sure that
     * the objects related to the current object are correct based on foreign key.
     *
     * You can override this method in the stub class, but you should always invoke
     * the base method from the overridden method (i.e. parent::ensureConsistency()),
     * in case your model changes.
     *
     * @throws PropelException
     */
    public function ensureConsistency()
    {

        if ($this->aPlayers !== null && $this->game_id !== $this->aPlayers->getGameId()) {
            $this->aPlayers = null;
        }
        if ($this->aPlayers !== null && $this->player_id !== $this->aPlayers->getId()) {
            $this->aPlayers = null;
        }
        if ($this->aProvinceDefs !== null && $this->province_def_id !== $this->aProvinceDefs->getId()) {
            $this->aProvinceDefs = null;
        }
    } // ensureConsistency

    /**
     * Reloads this object from datastore based on primary key and (optionally) resets all associated objects.
     *
     * This will only work if the object has been saved and has a valid primary key set.
     *
     * @param boolean $deep (optional) Whether to also de-associated any related objects.
     * @param PropelPDO $con (optional) The PropelPDO connection to use.
     * @return void
     * @throws PropelException - if this object is deleted, unsaved or doesn't have pk match in db
     */
    public function reload($deep = false, PropelPDO $con = null)
    {
        if ($this->isDeleted()) {
            throw new PropelException("Cannot reload a deleted object.");
        }

        if ($this->isNew()) {
            throw new PropelException("Cannot reload an unsaved object.");
        }

        if ($con === null) {
            $con = Propel::getConnection(ProvincesPeer::DATABASE_NAME, Propel::CONNECTION_READ);
        }

        // We don't need to alter the object instance pool; we're just modifying this instance
        // already in the pool.

        $stmt = ProvincesPeer::doSelectStmt($this->buildPkeyCriteria(), $con);
        $row = $stmt->fetch(PDO::FETCH_NUM);
        $stmt->closeCursor();
        if (!$row) {
            throw new PropelException('Cannot find matching row in the database to reload object values.');
        }
        $this->hydrate($row, 0, true); // rehydrate

        if ($deep) {  // also de-associate any related objects?

            $this->aProvinceDefs = null;
            $this->aPlayers = null;
            $this->collArmys = null;

            $this->collInfrastructuress = null;

            $this->collMaterialss = null;

            $this->collProvinceHistoriess = null;

            $this->collWarsRelatedByAttackerProvinceId = null;

            $this->collWarsRelatedByOpponentProvinceId = null;

        } // if (deep)
    }

    /**
     * Removes this object from datastore and sets delete attribute.
     *
     * @param PropelPDO $con
     * @return void
     * @throws PropelException
     * @throws Exception
     * @see        BaseObject::setDeleted()
     * @see        BaseObject::isDeleted()
     */
    public function delete(PropelPDO $con = null)
    {
        if ($this->isDeleted()) {
            throw new PropelException("This object has already been deleted.");
        }

        if ($con === null) {
            $con = Propel::getConnection(ProvincesPeer::DATABASE_NAME, Propel::CONNECTION_WRITE);
        }

        $con->beginTransaction();
        try {
            $deleteQuery = ProvincesQuery::create()
                ->filterByPrimaryKey($this->getPrimaryKey());
            $ret = $this->preDelete($con);
            if ($ret) {
                $deleteQuery->delete($con);
                $this->postDelete($con);
                $con->commit();
                $this->setDeleted(true);
            } else {
                $con->commit();
            }
        } catch (Exception $e) {
            $con->rollBack();
            throw $e;
        }
    }

    /**
     * Persists this object to the database.
     *
     * If the object is new, it inserts it; otherwise an update is performed.
     * All modified related objects will also be persisted in the doSave()
     * method.  This method wraps all precipitate database operations in a
     * single transaction.
     *
     * @param PropelPDO $con
     * @return int             The number of rows affected by this insert/update and any referring fk objects' save() operations.
     * @throws PropelException
     * @throws Exception
     * @see        doSave()
     */
    public function save(PropelPDO $con = null)
    {
        if ($this->isDeleted()) {
            throw new PropelException("You cannot save an object that has been deleted.");
        }

        if ($con === null) {
            $con = Propel::getConnection(ProvincesPeer::DATABASE_NAME, Propel::CONNECTION_WRITE);
        }

        $con->beginTransaction();
        $isInsert = $this->isNew();
        try {
            $ret = $this->preSave($con);
            if ($isInsert) {
                $ret = $ret && $this->preInsert($con);
            } else {
                $ret = $ret && $this->preUpdate($con);
            }
            if ($ret) {
                $affectedRows = $this->doSave($con);
                if ($isInsert) {
                    $this->postInsert($con);
                } else {
                    $this->postUpdate($con);
                }
                $this->postSave($con);
                ProvincesPeer::addInstanceToPool($this);
            } else {
                $affectedRows = 0;
            }
            $con->commit();

            return $affectedRows;
        } catch (Exception $e) {
            $con->rollBack();
            throw $e;
        }
    }

    /**
     * Performs the work of inserting or updating the row in the database.
     *
     * If the object is new, it inserts it; otherwise an update is performed.
     * All related objects are also updated in this method.
     *
     * @param PropelPDO $con
     * @return int             The number of rows affected by this insert/update and any referring fk objects' save() operations.
     * @throws PropelException
     * @see        save()
     */
    protected function doSave(PropelPDO $con)
    {
        $affectedRows = 0; // initialize var to track total num of affected rows
        if (!$this->alreadyInSave) {
            $this->alreadyInSave = true;

            // We call the save method on the following object(s) if they
            // were passed to this object by their coresponding set
            // method.  This object relates to these object(s) by a
            // foreign key reference.

            if ($this->aProvinceDefs !== null) {
                if ($this->aProvinceDefs->isModified() || $this->aProvinceDefs->isNew()) {
                    $affectedRows += $this->aProvinceDefs->save($con);
                }
                $this->setProvinceDefs($this->aProvinceDefs);
            }

            if ($this->aPlayers !== null) {
                if ($this->aPlayers->isModified() || $this->aPlayers->isNew()) {
                    $affectedRows += $this->aPlayers->save($con);
                }
                $this->setPlayers($this->aPlayers);
            }

            if ($this->isNew() || $this->isModified()) {
                // persist changes
                if ($this->isNew()) {
                    $this->doInsert($con);
                } else {
                    $this->doUpdate($con);
                }
                $affectedRows += 1;
                $this->resetModified();
            }

            if ($this->armysScheduledForDeletion !== null) {
                if (!$this->armysScheduledForDeletion->isEmpty()) {
                    ArmyQuery::create()
                        ->filterByPrimaryKeys($this->armysScheduledForDeletion->getPrimaryKeys(false))
                        ->delete($con);
                    $this->armysScheduledForDeletion = null;
                }
            }

            if ($this->collArmys !== null) {
                foreach ($this->collArmys as $referrerFK) {
                    if (!$referrerFK->isDeleted() && ($referrerFK->isNew() || $referrerFK->isModified())) {
                        $affectedRows += $referrerFK->save($con);
                    }
                }
            }

            if ($this->infrastructuressScheduledForDeletion !== null) {
                if (!$this->infrastructuressScheduledForDeletion->isEmpty()) {
                    InfrastructuresQuery::create()
                        ->filterByPrimaryKeys($this->infrastructuressScheduledForDeletion->getPrimaryKeys(false))
                        ->delete($con);
                    $this->infrastructuressScheduledForDeletion = null;
                }
            }

            if ($this->collInfrastructuress !== null) {
                foreach ($this->collInfrastructuress as $referrerFK) {
                    if (!$referrerFK->isDeleted() && ($referrerFK->isNew() || $referrerFK->isModified())) {
                        $affectedRows += $referrerFK->save($con);
                    }
                }
            }

            if ($this->materialssScheduledForDeletion !== null) {
                if (!$this->materialssScheduledForDeletion->isEmpty()) {
                    MaterialsQuery::create()
                        ->filterByPrimaryKeys($this->materialssScheduledForDeletion->getPrimaryKeys(false))
                        ->delete($con);
                    $this->materialssScheduledForDeletion = null;
                }
            }

            if ($this->collMaterialss !== null) {
                foreach ($this->collMaterialss as $referrerFK) {
                    if (!$referrerFK->isDeleted() && ($referrerFK->isNew() || $referrerFK->isModified())) {
                        $affectedRows += $referrerFK->save($con);
                    }
                }
            }

            if ($this->provinceHistoriessScheduledForDeletion !== null) {
                if (!$this->provinceHistoriessScheduledForDeletion->isEmpty()) {
                    ProvinceHistoriesQuery::create()
                        ->filterByPrimaryKeys($this->provinceHistoriessScheduledForDeletion->getPrimaryKeys(false))
                        ->delete($con);
                    $this->provinceHistoriessScheduledForDeletion = null;
                }
            }

            if ($this->collProvinceHistoriess !== null) {
                foreach ($this->collProvinceHistoriess as $referrerFK) {
                    if (!$referrerFK->isDeleted() && ($referrerFK->isNew() || $referrerFK->isModified())) {
                        $affectedRows += $referrerFK->save($con);
                    }
                }
            }

            if ($this->warsRelatedByAttackerProvinceIdScheduledForDeletion !== null) {
                if (!$this->warsRelatedByAttackerProvinceIdScheduledForDeletion->isEmpty()) {
                    WarQuery::create()
                        ->filterByPrimaryKeys($this->warsRelatedByAttackerProvinceIdScheduledForDeletion->getPrimaryKeys(false))
                        ->delete($con);
                    $this->warsRelatedByAttackerProvinceIdScheduledForDeletion = null;
                }
            }

            if ($this->collWarsRelatedByAttackerProvinceId !== null) {
                foreach ($this->collWarsRelatedByAttackerProvinceId as $referrerFK) {
                    if (!$referrerFK->isDeleted() && ($referrerFK->isNew() || $referrerFK->isModified())) {
                        $affectedRows += $referrerFK->save($con);
                    }
                }
            }

            if ($this->warsRelatedByOpponentProvinceIdScheduledForDeletion !== null) {
                if (!$this->warsRelatedByOpponentProvinceIdScheduledForDeletion->isEmpty()) {
                    WarQuery::create()
                        ->filterByPrimaryKeys($this->warsRelatedByOpponentProvinceIdScheduledForDeletion->getPrimaryKeys(false))
                        ->delete($con);
                    $this->warsRelatedByOpponentProvinceIdScheduledForDeletion = null;
                }
            }

            if ($this->collWarsRelatedByOpponentProvinceId !== null) {
                foreach ($this->collWarsRelatedByOpponentProvinceId as $referrerFK) {
                    if (!$referrerFK->isDeleted() && ($referrerFK->isNew() || $referrerFK->isModified())) {
                        $affectedRows += $referrerFK->save($con);
                    }
                }
            }

            $this->alreadyInSave = false;

        }

        return $affectedRows;
    } // doSave()

    /**
     * Insert the row in the database.
     *
     * @param PropelPDO $con
     *
     * @throws PropelException
     * @see        doSave()
     */
    protected function doInsert(PropelPDO $con)
    {
        $modifiedColumns = array();
        $index = 0;

        $this->modifiedColumns[] = ProvincesPeer::ID;
        if (null !== $this->id) {
            throw new PropelException('Cannot insert a value for auto-increment primary key (' . ProvincesPeer::ID . ')');
        }

         // check the columns in natural order for more readable SQL queries
        if ($this->isColumnModified(ProvincesPeer::ID)) {
            $modifiedColumns[':p' . $index++]  = '`id`';
        }
        if ($this->isColumnModified(ProvincesPeer::GAME_ID)) {
            $modifiedColumns[':p' . $index++]  = '`game_id`';
        }
        if ($this->isColumnModified(ProvincesPeer::PLAYER_ID)) {
            $modifiedColumns[':p' . $index++]  = '`player_id`';
        }
        if ($this->isColumnModified(ProvincesPeer::PROVINCE_DEF_ID)) {
            $modifiedColumns[':p' . $index++]  = '`province_def_id`';
        }
        if ($this->isColumnModified(ProvincesPeer::ACTIVE)) {
            $modifiedColumns[':p' . $index++]  = '`active`';
        }

        $sql = sprintf(
            'INSERT INTO `provinces` (%s) VALUES (%s)',
            implode(', ', $modifiedColumns),
            implode(', ', array_keys($modifiedColumns))
        );

        try {
            $stmt = $con->prepare($sql);
            foreach ($modifiedColumns as $identifier => $columnName) {
                switch ($columnName) {
                    case '`id`':
                        $stmt->bindValue($identifier, $this->id, PDO::PARAM_INT);
                        break;
                    case '`game_id`':
                        $stmt->bindValue($identifier, $this->game_id, PDO::PARAM_INT);
                        break;
                    case '`player_id`':
                        $stmt->bindValue($identifier, $this->player_id, PDO::PARAM_INT);
                        break;
                    case '`province_def_id`':
                        $stmt->bindValue($identifier, $this->province_def_id, PDO::PARAM_INT);
                        break;
                    case '`active`':
                        $stmt->bindValue($identifier, $this->active, PDO::PARAM_INT);
                        break;
                }
            }
            $stmt->execute();
        } catch (Exception $e) {
            Propel::log($e->getMessage(), Propel::LOG_ERR);
            throw new PropelException(sprintf('Unable to execute INSERT statement [%s]', $sql), $e);
        }

        try {
            $pk = $con->lastInsertId();
        } catch (Exception $e) {
            throw new PropelException('Unable to get autoincrement id.', $e);
        }
        $this->setId($pk);

        $this->setNew(false);
    }

    /**
     * Update the row in the database.
     *
     * @param PropelPDO $con
     *
     * @see        doSave()
     */
    protected function doUpdate(PropelPDO $con)
    {
        $selectCriteria = $this->buildPkeyCriteria();
        $valuesCriteria = $this->buildCriteria();
        BasePeer::doUpdate($selectCriteria, $valuesCriteria, $con);
    }

    /**
     * Array of ValidationFailed objects.
     * @var        array ValidationFailed[]
     */
    protected $validationFailures = array();

    /**
     * Gets any ValidationFailed objects that resulted from last call to validate().
     *
     *
     * @return array ValidationFailed[]
     * @see        validate()
     */
    public function getValidationFailures()
    {
        return $this->validationFailures;
    }

    /**
     * Validates the objects modified field values and all objects related to this table.
     *
     * If $columns is either a column name or an array of column names
     * only those columns are validated.
     *
     * @param mixed $columns Column name or an array of column names.
     * @return boolean Whether all columns pass validation.
     * @see        doValidate()
     * @see        getValidationFailures()
     */
    public function validate($columns = null)
    {
        $res = $this->doValidate($columns);
        if ($res === true) {
            $this->validationFailures = array();

            return true;
        }

        $this->validationFailures = $res;

        return false;
    }

    /**
     * This function performs the validation work for complex object models.
     *
     * In addition to checking the current object, all related objects will
     * also be validated.  If all pass then <code>true</code> is returned; otherwise
     * an aggreagated array of ValidationFailed objects will be returned.
     *
     * @param array $columns Array of column names to validate.
     * @return mixed <code>true</code> if all validations pass; array of <code>ValidationFailed</code> objets otherwise.
     */
    protected function doValidate($columns = null)
    {
        if (!$this->alreadyInValidation) {
            $this->alreadyInValidation = true;
            $retval = null;

            $failureMap = array();


            // We call the validate method on the following object(s) if they
            // were passed to this object by their coresponding set
            // method.  This object relates to these object(s) by a
            // foreign key reference.

            if ($this->aProvinceDefs !== null) {
                if (!$this->aProvinceDefs->validate($columns)) {
                    $failureMap = array_merge($failureMap, $this->aProvinceDefs->getValidationFailures());
                }
            }

            if ($this->aPlayers !== null) {
                if (!$this->aPlayers->validate($columns)) {
                    $failureMap = array_merge($failureMap, $this->aPlayers->getValidationFailures());
                }
            }


            if (($retval = ProvincesPeer::doValidate($this, $columns)) !== true) {
                $failureMap = array_merge($failureMap, $retval);
            }


                if ($this->collArmys !== null) {
                    foreach ($this->collArmys as $referrerFK) {
                        if (!$referrerFK->validate($columns)) {
                            $failureMap = array_merge($failureMap, $referrerFK->getValidationFailures());
                        }
                    }
                }

                if ($this->collInfrastructuress !== null) {
                    foreach ($this->collInfrastructuress as $referrerFK) {
                        if (!$referrerFK->validate($columns)) {
                            $failureMap = array_merge($failureMap, $referrerFK->getValidationFailures());
                        }
                    }
                }

                if ($this->collMaterialss !== null) {
                    foreach ($this->collMaterialss as $referrerFK) {
                        if (!$referrerFK->validate($columns)) {
                            $failureMap = array_merge($failureMap, $referrerFK->getValidationFailures());
                        }
                    }
                }

                if ($this->collProvinceHistoriess !== null) {
                    foreach ($this->collProvinceHistoriess as $referrerFK) {
                        if (!$referrerFK->validate($columns)) {
                            $failureMap = array_merge($failureMap, $referrerFK->getValidationFailures());
                        }
                    }
                }

                if ($this->collWarsRelatedByAttackerProvinceId !== null) {
                    foreach ($this->collWarsRelatedByAttackerProvinceId as $referrerFK) {
                        if (!$referrerFK->validate($columns)) {
                            $failureMap = array_merge($failureMap, $referrerFK->getValidationFailures());
                        }
                    }
                }

                if ($this->collWarsRelatedByOpponentProvinceId !== null) {
                    foreach ($this->collWarsRelatedByOpponentProvinceId as $referrerFK) {
                        if (!$referrerFK->validate($columns)) {
                            $failureMap = array_merge($failureMap, $referrerFK->getValidationFailures());
                        }
                    }
                }


            $this->alreadyInValidation = false;
        }

        return (!empty($failureMap) ? $failureMap : true);
    }

    /**
     * Retrieves a field from the object by name passed in as a string.
     *
     * @param string $name name
     * @param string $type The type of fieldname the $name is of:
     *               one of the class type constants BasePeer::TYPE_PHPNAME, BasePeer::TYPE_STUDLYPHPNAME
     *               BasePeer::TYPE_COLNAME, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_NUM.
     *               Defaults to BasePeer::TYPE_PHPNAME
     * @return mixed Value of field.
     */
    public function getByName($name, $type = BasePeer::TYPE_PHPNAME)
    {
        $pos = ProvincesPeer::translateFieldName($name, $type, BasePeer::TYPE_NUM);
        $field = $this->getByPosition($pos);

        return $field;
    }

    /**
     * Retrieves a field from the object by Position as specified in the xml schema.
     * Zero-based.
     *
     * @param int $pos position in xml schema
     * @return mixed Value of field at $pos
     */
    public function getByPosition($pos)
    {
        switch ($pos) {
            case 0:
                return $this->getId();
                break;
            case 1:
                return $this->getGameId();
                break;
            case 2:
                return $this->getPlayerId();
                break;
            case 3:
                return $this->getProvinceDefId();
                break;
            case 4:
                return $this->getActive();
                break;
            default:
                return null;
                break;
        } // switch()
    }

    /**
     * Exports the object as an array.
     *
     * You can specify the key type of the array by passing one of the class
     * type constants.
     *
     * @param     string  $keyType (optional) One of the class type constants BasePeer::TYPE_PHPNAME, BasePeer::TYPE_STUDLYPHPNAME,
     *                    BasePeer::TYPE_COLNAME, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_NUM.
     *                    Defaults to BasePeer::TYPE_PHPNAME.
     * @param     boolean $includeLazyLoadColumns (optional) Whether to include lazy loaded columns. Defaults to true.
     * @param     array $alreadyDumpedObjects List of objects to skip to avoid recursion
     * @param     boolean $includeForeignObjects (optional) Whether to include hydrated related objects. Default to FALSE.
     *
     * @return array an associative array containing the field names (as keys) and field values
     */
    public function toArray($keyType = BasePeer::TYPE_PHPNAME, $includeLazyLoadColumns = true, $alreadyDumpedObjects = array(), $includeForeignObjects = false)
    {
        if (isset($alreadyDumpedObjects['Provinces'][$this->getPrimaryKey()])) {
            return '*RECURSION*';
        }
        $alreadyDumpedObjects['Provinces'][$this->getPrimaryKey()] = true;
        $keys = ProvincesPeer::getFieldNames($keyType);
        $result = array(
            $keys[0] => $this->getId(),
            $keys[1] => $this->getGameId(),
            $keys[2] => $this->getPlayerId(),
            $keys[3] => $this->getProvinceDefId(),
            $keys[4] => $this->getActive(),
        );
        if ($includeForeignObjects) {
            if (null !== $this->aProvinceDefs) {
                $result['ProvinceDefs'] = $this->aProvinceDefs->toArray($keyType, $includeLazyLoadColumns,  $alreadyDumpedObjects, true);
            }
            if (null !== $this->aPlayers) {
                $result['Players'] = $this->aPlayers->toArray($keyType, $includeLazyLoadColumns,  $alreadyDumpedObjects, true);
            }
            if (null !== $this->collArmys) {
                $result['Armys'] = $this->collArmys->toArray(null, true, $keyType, $includeLazyLoadColumns, $alreadyDumpedObjects);
            }
            if (null !== $this->collInfrastructuress) {
                $result['Infrastructuress'] = $this->collInfrastructuress->toArray(null, true, $keyType, $includeLazyLoadColumns, $alreadyDumpedObjects);
            }
            if (null !== $this->collMaterialss) {
                $result['Materialss'] = $this->collMaterialss->toArray(null, true, $keyType, $includeLazyLoadColumns, $alreadyDumpedObjects);
            }
            if (null !== $this->collProvinceHistoriess) {
                $result['ProvinceHistoriess'] = $this->collProvinceHistoriess->toArray(null, true, $keyType, $includeLazyLoadColumns, $alreadyDumpedObjects);
            }
            if (null !== $this->collWarsRelatedByAttackerProvinceId) {
                $result['WarsRelatedByAttackerProvinceId'] = $this->collWarsRelatedByAttackerProvinceId->toArray(null, true, $keyType, $includeLazyLoadColumns, $alreadyDumpedObjects);
            }
            if (null !== $this->collWarsRelatedByOpponentProvinceId) {
                $result['WarsRelatedByOpponentProvinceId'] = $this->collWarsRelatedByOpponentProvinceId->toArray(null, true, $keyType, $includeLazyLoadColumns, $alreadyDumpedObjects);
            }
        }

        return $result;
    }

    /**
     * Sets a field from the object by name passed in as a string.
     *
     * @param string $name peer name
     * @param mixed $value field value
     * @param string $type The type of fieldname the $name is of:
     *                     one of the class type constants BasePeer::TYPE_PHPNAME, BasePeer::TYPE_STUDLYPHPNAME
     *                     BasePeer::TYPE_COLNAME, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_NUM.
     *                     Defaults to BasePeer::TYPE_PHPNAME
     * @return void
     */
    public function setByName($name, $value, $type = BasePeer::TYPE_PHPNAME)
    {
        $pos = ProvincesPeer::translateFieldName($name, $type, BasePeer::TYPE_NUM);

        $this->setByPosition($pos, $value);
    }

    /**
     * Sets a field from the object by Position as specified in the xml schema.
     * Zero-based.
     *
     * @param int $pos position in xml schema
     * @param mixed $value field value
     * @return void
     */
    public function setByPosition($pos, $value)
    {
        switch ($pos) {
            case 0:
                $this->setId($value);
                break;
            case 1:
                $this->setGameId($value);
                break;
            case 2:
                $this->setPlayerId($value);
                break;
            case 3:
                $this->setProvinceDefId($value);
                break;
            case 4:
                $this->setActive($value);
                break;
        } // switch()
    }

    /**
     * Populates the object using an array.
     *
     * This is particularly useful when populating an object from one of the
     * request arrays (e.g. $_POST).  This method goes through the column
     * names, checking to see whether a matching key exists in populated
     * array. If so the setByName() method is called for that column.
     *
     * You can specify the key type of the array by additionally passing one
     * of the class type constants BasePeer::TYPE_PHPNAME, BasePeer::TYPE_STUDLYPHPNAME,
     * BasePeer::TYPE_COLNAME, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_NUM.
     * The default key type is the column's BasePeer::TYPE_PHPNAME
     *
     * @param array  $arr     An array to populate the object from.
     * @param string $keyType The type of keys the array uses.
     * @return void
     */
    public function fromArray($arr, $keyType = BasePeer::TYPE_PHPNAME)
    {
        $keys = ProvincesPeer::getFieldNames($keyType);

        if (array_key_exists($keys[0], $arr)) $this->setId($arr[$keys[0]]);
        if (array_key_exists($keys[1], $arr)) $this->setGameId($arr[$keys[1]]);
        if (array_key_exists($keys[2], $arr)) $this->setPlayerId($arr[$keys[2]]);
        if (array_key_exists($keys[3], $arr)) $this->setProvinceDefId($arr[$keys[3]]);
        if (array_key_exists($keys[4], $arr)) $this->setActive($arr[$keys[4]]);
    }

    /**
     * Build a Criteria object containing the values of all modified columns in this object.
     *
     * @return Criteria The Criteria object containing all modified values.
     */
    public function buildCriteria()
    {
        $criteria = new Criteria(ProvincesPeer::DATABASE_NAME);

        if ($this->isColumnModified(ProvincesPeer::ID)) $criteria->add(ProvincesPeer::ID, $this->id);
        if ($this->isColumnModified(ProvincesPeer::GAME_ID)) $criteria->add(ProvincesPeer::GAME_ID, $this->game_id);
        if ($this->isColumnModified(ProvincesPeer::PLAYER_ID)) $criteria->add(ProvincesPeer::PLAYER_ID, $this->player_id);
        if ($this->isColumnModified(ProvincesPeer::PROVINCE_DEF_ID)) $criteria->add(ProvincesPeer::PROVINCE_DEF_ID, $this->province_def_id);
        if ($this->isColumnModified(ProvincesPeer::ACTIVE)) $criteria->add(ProvincesPeer::ACTIVE, $this->active);

        return $criteria;
    }

    /**
     * Builds a Criteria object containing the primary key for this object.
     *
     * Unlike buildCriteria() this method includes the primary key values regardless
     * of whether or not they have been modified.
     *
     * @return Criteria The Criteria object containing value(s) for primary key(s).
     */
    public function buildPkeyCriteria()
    {
        $criteria = new Criteria(ProvincesPeer::DATABASE_NAME);
        $criteria->add(ProvincesPeer::ID, $this->id);

        return $criteria;
    }

    /**
     * Returns the primary key for this object (row).
     * @return int
     */
    public function getPrimaryKey()
    {
        return $this->getId();
    }

    /**
     * Generic method to set the primary key (id column).
     *
     * @param  int $key Primary key.
     * @return void
     */
    public function setPrimaryKey($key)
    {
        $this->setId($key);
    }

    /**
     * Returns true if the primary key for this object is null.
     * @return boolean
     */
    public function isPrimaryKeyNull()
    {

        return null === $this->getId();
    }

    /**
     * Sets contents of passed object to values from current object.
     *
     * If desired, this method can also make copies of all associated (fkey referrers)
     * objects.
     *
     * @param object $copyObj An object of Provinces (or compatible) type.
     * @param boolean $deepCopy Whether to also copy all rows that refer (by fkey) to the current row.
     * @param boolean $makeNew Whether to reset autoincrement PKs and make the object new.
     * @throws PropelException
     */
    public function copyInto($copyObj, $deepCopy = false, $makeNew = true)
    {
        $copyObj->setGameId($this->getGameId());
        $copyObj->setPlayerId($this->getPlayerId());
        $copyObj->setProvinceDefId($this->getProvinceDefId());
        $copyObj->setActive($this->getActive());

        if ($deepCopy && !$this->startCopy) {
            // important: temporarily setNew(false) because this affects the behavior of
            // the getter/setter methods for fkey referrer objects.
            $copyObj->setNew(false);
            // store object hash to prevent cycle
            $this->startCopy = true;

            foreach ($this->getArmys() as $relObj) {
                if ($relObj !== $this) {  // ensure that we don't try to copy a reference to ourselves
                    $copyObj->addArmy($relObj->copy($deepCopy));
                }
            }

            foreach ($this->getInfrastructuress() as $relObj) {
                if ($relObj !== $this) {  // ensure that we don't try to copy a reference to ourselves
                    $copyObj->addInfrastructures($relObj->copy($deepCopy));
                }
            }

            foreach ($this->getMaterialss() as $relObj) {
                if ($relObj !== $this) {  // ensure that we don't try to copy a reference to ourselves
                    $copyObj->addMaterials($relObj->copy($deepCopy));
                }
            }

            foreach ($this->getProvinceHistoriess() as $relObj) {
                if ($relObj !== $this) {  // ensure that we don't try to copy a reference to ourselves
                    $copyObj->addProvinceHistories($relObj->copy($deepCopy));
                }
            }

            foreach ($this->getWarsRelatedByAttackerProvinceId() as $relObj) {
                if ($relObj !== $this) {  // ensure that we don't try to copy a reference to ourselves
                    $copyObj->addWarRelatedByAttackerProvinceId($relObj->copy($deepCopy));
                }
            }

            foreach ($this->getWarsRelatedByOpponentProvinceId() as $relObj) {
                if ($relObj !== $this) {  // ensure that we don't try to copy a reference to ourselves
                    $copyObj->addWarRelatedByOpponentProvinceId($relObj->copy($deepCopy));
                }
            }

            //unflag object copy
            $this->startCopy = false;
        } // if ($deepCopy)

        if ($makeNew) {
            $copyObj->setNew(true);
            $copyObj->setId(NULL); // this is a auto-increment column, so set to default value
        }
    }

    /**
     * Makes a copy of this object that will be inserted as a new row in table when saved.
     * It creates a new object filling in the simple attributes, but skipping any primary
     * keys that are defined for the table.
     *
     * If desired, this method can also make copies of all associated (fkey referrers)
     * objects.
     *
     * @param boolean $deepCopy Whether to also copy all rows that refer (by fkey) to the current row.
     * @return Provinces Clone of current object.
     * @throws PropelException
     */
    public function copy($deepCopy = false)
    {
        // we use get_class(), because this might be a subclass
        $clazz = get_class($this);
        $copyObj = new $clazz();
        $this->copyInto($copyObj, $deepCopy);

        return $copyObj;
    }

    /**
     * Returns a peer instance associated with this om.
     *
     * Since Peer classes are not to have any instance attributes, this method returns the
     * same instance for all member of this class. The method could therefore
     * be static, but this would prevent one from overriding the behavior.
     *
     * @return ProvincesPeer
     */
    public function getPeer()
    {
        if (self::$peer === null) {
            self::$peer = new ProvincesPeer();
        }

        return self::$peer;
    }

    /**
     * Declares an association between this object and a ProvinceDefs object.
     *
     * @param             ProvinceDefs $v
     * @return Provinces The current object (for fluent API support)
     * @throws PropelException
     */
    public function setProvinceDefs(ProvinceDefs $v = null)
    {
        if ($v === null) {
            $this->setProvinceDefId(NULL);
        } else {
            $this->setProvinceDefId($v->getId());
        }

        $this->aProvinceDefs = $v;

        // Add binding for other direction of this n:n relationship.
        // If this object has already been added to the ProvinceDefs object, it will not be re-added.
        if ($v !== null) {
            $v->addProvinces($this);
        }


        return $this;
    }


    /**
     * Get the associated ProvinceDefs object
     *
     * @param PropelPDO $con Optional Connection object.
     * @param $doQuery Executes a query to get the object if required
     * @return ProvinceDefs The associated ProvinceDefs object.
     * @throws PropelException
     */
    public function getProvinceDefs(PropelPDO $con = null, $doQuery = true)
    {
        if ($this->aProvinceDefs === null && ($this->province_def_id !== null) && $doQuery) {
            $this->aProvinceDefs = ProvinceDefsQuery::create()->findPk($this->province_def_id, $con);
            /* The following can be used additionally to
                guarantee the related object contains a reference
                to this object.  This level of coupling may, however, be
                undesirable since it could result in an only partially populated collection
                in the referenced object.
                $this->aProvinceDefs->addProvincess($this);
             */
        }

        return $this->aProvinceDefs;
    }

    /**
     * Declares an association between this object and a Players object.
     *
     * @param             Players $v
     * @return Provinces The current object (for fluent API support)
     * @throws PropelException
     */
    public function setPlayers(Players $v = null)
    {
        if ($v === null) {
            $this->setPlayerId(NULL);
        } else {
            $this->setPlayerId($v->getId());
        }

        if ($v === null) {
            $this->setGameId(NULL);
        } else {
            $this->setGameId($v->getGameId());
        }

        $this->aPlayers = $v;

        // Add binding for other direction of this n:n relationship.
        // If this object has already been added to the Players object, it will not be re-added.
        if ($v !== null) {
            $v->addProvinces($this);
        }


        return $this;
    }


    /**
     * Get the associated Players object
     *
     * @param PropelPDO $con Optional Connection object.
     * @param $doQuery Executes a query to get the object if required
     * @return Players The associated Players object.
     * @throws PropelException
     */
    public function getPlayers(PropelPDO $con = null, $doQuery = true)
    {
        if ($this->aPlayers === null && ($this->player_id !== null && $this->game_id !== null) && $doQuery) {
            $this->aPlayers = PlayersQuery::create()->findPk(array($this->player_id, $this->game_id), $con);
            /* The following can be used additionally to
                guarantee the related object contains a reference
                to this object.  This level of coupling may, however, be
                undesirable since it could result in an only partially populated collection
                in the referenced object.
                $this->aPlayers->addProvincess($this);
             */
        }

        return $this->aPlayers;
    }


    /**
     * Initializes a collection based on the name of a relation.
     * Avoids crafting an 'init[$relationName]s' method name
     * that wouldn't work when StandardEnglishPluralizer is used.
     *
     * @param string $relationName The name of the relation to initialize
     * @return void
     */
    public function initRelation($relationName)
    {
        if ('Army' == $relationName) {
            $this->initArmys();
        }
        if ('Infrastructures' == $relationName) {
            $this->initInfrastructuress();
        }
        if ('Materials' == $relationName) {
            $this->initMaterialss();
        }
        if ('ProvinceHistories' == $relationName) {
            $this->initProvinceHistoriess();
        }
        if ('WarRelatedByAttackerProvinceId' == $relationName) {
            $this->initWarsRelatedByAttackerProvinceId();
        }
        if ('WarRelatedByOpponentProvinceId' == $relationName) {
            $this->initWarsRelatedByOpponentProvinceId();
        }
    }

    /**
     * Clears out the collArmys collection
     *
     * This does not modify the database; however, it will remove any associated objects, causing
     * them to be refetched by subsequent calls to accessor method.
     *
     * @return Provinces The current object (for fluent API support)
     * @see        addArmys()
     */
    public function clearArmys()
    {
        $this->collArmys = null; // important to set this to null since that means it is uninitialized
        $this->collArmysPartial = null;

        return $this;
    }

    /**
     * reset is the collArmys collection loaded partially
     *
     * @return void
     */
    public function resetPartialArmys($v = true)
    {
        $this->collArmysPartial = $v;
    }

    /**
     * Initializes the collArmys collection.
     *
     * By default this just sets the collArmys collection to an empty array (like clearcollArmys());
     * however, you may wish to override this method in your stub class to provide setting appropriate
     * to your application -- for example, setting the initial array to the values stored in database.
     *
     * @param boolean $overrideExisting If set to true, the method call initializes
     *                                        the collection even if it is not empty
     *
     * @return void
     */
    public function initArmys($overrideExisting = true)
    {
        if (null !== $this->collArmys && !$overrideExisting) {
            return;
        }
        $this->collArmys = new PropelObjectCollection();
        $this->collArmys->setModel('Army');
    }

    /**
     * Gets an array of Army objects which contain a foreign key that references this object.
     *
     * If the $criteria is not null, it is used to always fetch the results from the database.
     * Otherwise the results are fetched from the database the first time, then cached.
     * Next time the same method is called without $criteria, the cached collection is returned.
     * If this Provinces is new, it will return
     * an empty collection or the current collection; the criteria is ignored on a new object.
     *
     * @param Criteria $criteria optional Criteria object to narrow the query
     * @param PropelPDO $con optional connection object
     * @return PropelObjectCollection|Army[] List of Army objects
     * @throws PropelException
     */
    public function getArmys($criteria = null, PropelPDO $con = null)
    {
        $partial = $this->collArmysPartial && !$this->isNew();
        if (null === $this->collArmys || null !== $criteria  || $partial) {
            if ($this->isNew() && null === $this->collArmys) {
                // return empty collection
                $this->initArmys();
            } else {
                $collArmys = ArmyQuery::create(null, $criteria)
                    ->filterByProvinces($this)
                    ->find($con);
                if (null !== $criteria) {
                    if (false !== $this->collArmysPartial && count($collArmys)) {
                      $this->initArmys(false);

                      foreach($collArmys as $obj) {
                        if (false == $this->collArmys->contains($obj)) {
                          $this->collArmys->append($obj);
                        }
                      }

                      $this->collArmysPartial = true;
                    }

                    $collArmys->getInternalIterator()->rewind();
                    return $collArmys;
                }

                if($partial && $this->collArmys) {
                    foreach($this->collArmys as $obj) {
                        if($obj->isNew()) {
                            $collArmys[] = $obj;
                        }
                    }
                }

                $this->collArmys = $collArmys;
                $this->collArmysPartial = false;
            }
        }

        return $this->collArmys;
    }

    /**
     * Sets a collection of Army objects related by a one-to-many relationship
     * to the current object.
     * It will also schedule objects for deletion based on a diff between old objects (aka persisted)
     * and new objects from the given Propel collection.
     *
     * @param PropelCollection $armys A Propel collection.
     * @param PropelPDO $con Optional connection object
     * @return Provinces The current object (for fluent API support)
     */
    public function setArmys(PropelCollection $armys, PropelPDO $con = null)
    {
        $armysToDelete = $this->getArmys(new Criteria(), $con)->diff($armys);

        $this->armysScheduledForDeletion = unserialize(serialize($armysToDelete));

        foreach ($armysToDelete as $armyRemoved) {
            $armyRemoved->setProvinces(null);
        }

        $this->collArmys = null;
        foreach ($armys as $army) {
            $this->addArmy($army);
        }

        $this->collArmys = $armys;
        $this->collArmysPartial = false;

        return $this;
    }

    /**
     * Returns the number of related Army objects.
     *
     * @param Criteria $criteria
     * @param boolean $distinct
     * @param PropelPDO $con
     * @return int             Count of related Army objects.
     * @throws PropelException
     */
    public function countArmys(Criteria $criteria = null, $distinct = false, PropelPDO $con = null)
    {
        $partial = $this->collArmysPartial && !$this->isNew();
        if (null === $this->collArmys || null !== $criteria || $partial) {
            if ($this->isNew() && null === $this->collArmys) {
                return 0;
            }

            if($partial && !$criteria) {
                return count($this->getArmys());
            }
            $query = ArmyQuery::create(null, $criteria);
            if ($distinct) {
                $query->distinct();
            }

            return $query
                ->filterByProvinces($this)
                ->count($con);
        }

        return count($this->collArmys);
    }

    /**
     * Method called to associate a Army object to this object
     * through the Army foreign key attribute.
     *
     * @param    Army $l Army
     * @return Provinces The current object (for fluent API support)
     */
    public function addArmy(Army $l)
    {
        if ($this->collArmys === null) {
            $this->initArmys();
            $this->collArmysPartial = true;
        }
        if (!in_array($l, $this->collArmys->getArrayCopy(), true)) { // only add it if the **same** object is not already associated
            $this->doAddArmy($l);
        }

        return $this;
    }

    /**
     * @param	Army $army The army object to add.
     */
    protected function doAddArmy($army)
    {
        $this->collArmys[]= $army;
        $army->setProvinces($this);
    }

    /**
     * @param	Army $army The army object to remove.
     * @return Provinces The current object (for fluent API support)
     */
    public function removeArmy($army)
    {
        if ($this->getArmys()->contains($army)) {
            $this->collArmys->remove($this->collArmys->search($army));
            if (null === $this->armysScheduledForDeletion) {
                $this->armysScheduledForDeletion = clone $this->collArmys;
                $this->armysScheduledForDeletion->clear();
            }
            $this->armysScheduledForDeletion[]= clone $army;
            $army->setProvinces(null);
        }

        return $this;
    }


    /**
     * If this collection has already been initialized with
     * an identical criteria, it returns the collection.
     * Otherwise if this Provinces is new, it will return
     * an empty collection; or if this Provinces has previously
     * been saved, it will retrieve related Armys from storage.
     *
     * This method is protected by default in order to keep the public
     * api reasonable.  You can provide public methods for those you
     * actually need in Provinces.
     *
     * @param Criteria $criteria optional Criteria object to narrow the query
     * @param PropelPDO $con optional connection object
     * @param string $join_behavior optional join type to use (defaults to Criteria::LEFT_JOIN)
     * @return PropelObjectCollection|Army[] List of Army objects
     */
    public function getArmysJoinArmyDefs($criteria = null, $con = null, $join_behavior = Criteria::LEFT_JOIN)
    {
        $query = ArmyQuery::create(null, $criteria);
        $query->joinWith('ArmyDefs', $join_behavior);

        return $this->getArmys($query, $con);
    }

    /**
     * Clears out the collInfrastructuress collection
     *
     * This does not modify the database; however, it will remove any associated objects, causing
     * them to be refetched by subsequent calls to accessor method.
     *
     * @return Provinces The current object (for fluent API support)
     * @see        addInfrastructuress()
     */
    public function clearInfrastructuress()
    {
        $this->collInfrastructuress = null; // important to set this to null since that means it is uninitialized
        $this->collInfrastructuressPartial = null;

        return $this;
    }

    /**
     * reset is the collInfrastructuress collection loaded partially
     *
     * @return void
     */
    public function resetPartialInfrastructuress($v = true)
    {
        $this->collInfrastructuressPartial = $v;
    }

    /**
     * Initializes the collInfrastructuress collection.
     *
     * By default this just sets the collInfrastructuress collection to an empty array (like clearcollInfrastructuress());
     * however, you may wish to override this method in your stub class to provide setting appropriate
     * to your application -- for example, setting the initial array to the values stored in database.
     *
     * @param boolean $overrideExisting If set to true, the method call initializes
     *                                        the collection even if it is not empty
     *
     * @return void
     */
    public function initInfrastructuress($overrideExisting = true)
    {
        if (null !== $this->collInfrastructuress && !$overrideExisting) {
            return;
        }
        $this->collInfrastructuress = new PropelObjectCollection();
        $this->collInfrastructuress->setModel('Infrastructures');
    }

    /**
     * Gets an array of Infrastructures objects which contain a foreign key that references this object.
     *
     * If the $criteria is not null, it is used to always fetch the results from the database.
     * Otherwise the results are fetched from the database the first time, then cached.
     * Next time the same method is called without $criteria, the cached collection is returned.
     * If this Provinces is new, it will return
     * an empty collection or the current collection; the criteria is ignored on a new object.
     *
     * @param Criteria $criteria optional Criteria object to narrow the query
     * @param PropelPDO $con optional connection object
     * @return PropelObjectCollection|Infrastructures[] List of Infrastructures objects
     * @throws PropelException
     */
    public function getInfrastructuress($criteria = null, PropelPDO $con = null)
    {
        $partial = $this->collInfrastructuressPartial && !$this->isNew();
        if (null === $this->collInfrastructuress || null !== $criteria  || $partial) {
            if ($this->isNew() && null === $this->collInfrastructuress) {
                // return empty collection
                $this->initInfrastructuress();
            } else {
                $collInfrastructuress = InfrastructuresQuery::create(null, $criteria)
                    ->filterByProvinces($this)
                    ->find($con);
                if (null !== $criteria) {
                    if (false !== $this->collInfrastructuressPartial && count($collInfrastructuress)) {
                      $this->initInfrastructuress(false);

                      foreach($collInfrastructuress as $obj) {
                        if (false == $this->collInfrastructuress->contains($obj)) {
                          $this->collInfrastructuress->append($obj);
                        }
                      }

                      $this->collInfrastructuressPartial = true;
                    }

                    $collInfrastructuress->getInternalIterator()->rewind();
                    return $collInfrastructuress;
                }

                if($partial && $this->collInfrastructuress) {
                    foreach($this->collInfrastructuress as $obj) {
                        if($obj->isNew()) {
                            $collInfrastructuress[] = $obj;
                        }
                    }
                }

                $this->collInfrastructuress = $collInfrastructuress;
                $this->collInfrastructuressPartial = false;
            }
        }

        return $this->collInfrastructuress;
    }

    /**
     * Sets a collection of Infrastructures objects related by a one-to-many relationship
     * to the current object.
     * It will also schedule objects for deletion based on a diff between old objects (aka persisted)
     * and new objects from the given Propel collection.
     *
     * @param PropelCollection $infrastructuress A Propel collection.
     * @param PropelPDO $con Optional connection object
     * @return Provinces The current object (for fluent API support)
     */
    public function setInfrastructuress(PropelCollection $infrastructuress, PropelPDO $con = null)
    {
        $infrastructuressToDelete = $this->getInfrastructuress(new Criteria(), $con)->diff($infrastructuress);

        $this->infrastructuressScheduledForDeletion = unserialize(serialize($infrastructuressToDelete));

        foreach ($infrastructuressToDelete as $infrastructuresRemoved) {
            $infrastructuresRemoved->setProvinces(null);
        }

        $this->collInfrastructuress = null;
        foreach ($infrastructuress as $infrastructures) {
            $this->addInfrastructures($infrastructures);
        }

        $this->collInfrastructuress = $infrastructuress;
        $this->collInfrastructuressPartial = false;

        return $this;
    }

    /**
     * Returns the number of related Infrastructures objects.
     *
     * @param Criteria $criteria
     * @param boolean $distinct
     * @param PropelPDO $con
     * @return int             Count of related Infrastructures objects.
     * @throws PropelException
     */
    public function countInfrastructuress(Criteria $criteria = null, $distinct = false, PropelPDO $con = null)
    {
        $partial = $this->collInfrastructuressPartial && !$this->isNew();
        if (null === $this->collInfrastructuress || null !== $criteria || $partial) {
            if ($this->isNew() && null === $this->collInfrastructuress) {
                return 0;
            }

            if($partial && !$criteria) {
                return count($this->getInfrastructuress());
            }
            $query = InfrastructuresQuery::create(null, $criteria);
            if ($distinct) {
                $query->distinct();
            }

            return $query
                ->filterByProvinces($this)
                ->count($con);
        }

        return count($this->collInfrastructuress);
    }

    /**
     * Method called to associate a Infrastructures object to this object
     * through the Infrastructures foreign key attribute.
     *
     * @param    Infrastructures $l Infrastructures
     * @return Provinces The current object (for fluent API support)
     */
    public function addInfrastructures(Infrastructures $l)
    {
        if ($this->collInfrastructuress === null) {
            $this->initInfrastructuress();
            $this->collInfrastructuressPartial = true;
        }
        if (!in_array($l, $this->collInfrastructuress->getArrayCopy(), true)) { // only add it if the **same** object is not already associated
            $this->doAddInfrastructures($l);
        }

        return $this;
    }

    /**
     * @param	Infrastructures $infrastructures The infrastructures object to add.
     */
    protected function doAddInfrastructures($infrastructures)
    {
        $this->collInfrastructuress[]= $infrastructures;
        $infrastructures->setProvinces($this);
    }

    /**
     * @param	Infrastructures $infrastructures The infrastructures object to remove.
     * @return Provinces The current object (for fluent API support)
     */
    public function removeInfrastructures($infrastructures)
    {
        if ($this->getInfrastructuress()->contains($infrastructures)) {
            $this->collInfrastructuress->remove($this->collInfrastructuress->search($infrastructures));
            if (null === $this->infrastructuressScheduledForDeletion) {
                $this->infrastructuressScheduledForDeletion = clone $this->collInfrastructuress;
                $this->infrastructuressScheduledForDeletion->clear();
            }
            $this->infrastructuressScheduledForDeletion[]= clone $infrastructures;
            $infrastructures->setProvinces(null);
        }

        return $this;
    }


    /**
     * If this collection has already been initialized with
     * an identical criteria, it returns the collection.
     * Otherwise if this Provinces is new, it will return
     * an empty collection; or if this Provinces has previously
     * been saved, it will retrieve related Infrastructuress from storage.
     *
     * This method is protected by default in order to keep the public
     * api reasonable.  You can provide public methods for those you
     * actually need in Provinces.
     *
     * @param Criteria $criteria optional Criteria object to narrow the query
     * @param PropelPDO $con optional connection object
     * @param string $join_behavior optional join type to use (defaults to Criteria::LEFT_JOIN)
     * @return PropelObjectCollection|Infrastructures[] List of Infrastructures objects
     */
    public function getInfrastructuressJoinInfrastructureDefs($criteria = null, $con = null, $join_behavior = Criteria::LEFT_JOIN)
    {
        $query = InfrastructuresQuery::create(null, $criteria);
        $query->joinWith('InfrastructureDefs', $join_behavior);

        return $this->getInfrastructuress($query, $con);
    }

    /**
     * Clears out the collMaterialss collection
     *
     * This does not modify the database; however, it will remove any associated objects, causing
     * them to be refetched by subsequent calls to accessor method.
     *
     * @return Provinces The current object (for fluent API support)
     * @see        addMaterialss()
     */
    public function clearMaterialss()
    {
        $this->collMaterialss = null; // important to set this to null since that means it is uninitialized
        $this->collMaterialssPartial = null;

        return $this;
    }

    /**
     * reset is the collMaterialss collection loaded partially
     *
     * @return void
     */
    public function resetPartialMaterialss($v = true)
    {
        $this->collMaterialssPartial = $v;
    }

    /**
     * Initializes the collMaterialss collection.
     *
     * By default this just sets the collMaterialss collection to an empty array (like clearcollMaterialss());
     * however, you may wish to override this method in your stub class to provide setting appropriate
     * to your application -- for example, setting the initial array to the values stored in database.
     *
     * @param boolean $overrideExisting If set to true, the method call initializes
     *                                        the collection even if it is not empty
     *
     * @return void
     */
    public function initMaterialss($overrideExisting = true)
    {
        if (null !== $this->collMaterialss && !$overrideExisting) {
            return;
        }
        $this->collMaterialss = new PropelObjectCollection();
        $this->collMaterialss->setModel('Materials');
    }

    /**
     * Gets an array of Materials objects which contain a foreign key that references this object.
     *
     * If the $criteria is not null, it is used to always fetch the results from the database.
     * Otherwise the results are fetched from the database the first time, then cached.
     * Next time the same method is called without $criteria, the cached collection is returned.
     * If this Provinces is new, it will return
     * an empty collection or the current collection; the criteria is ignored on a new object.
     *
     * @param Criteria $criteria optional Criteria object to narrow the query
     * @param PropelPDO $con optional connection object
     * @return PropelObjectCollection|Materials[] List of Materials objects
     * @throws PropelException
     */
    public function getMaterialss($criteria = null, PropelPDO $con = null)
    {
        $partial = $this->collMaterialssPartial && !$this->isNew();
        if (null === $this->collMaterialss || null !== $criteria  || $partial) {
            if ($this->isNew() && null === $this->collMaterialss) {
                // return empty collection
                $this->initMaterialss();
            } else {
                $collMaterialss = MaterialsQuery::create(null, $criteria)
                    ->filterByProvinces($this)
                    ->find($con);
                if (null !== $criteria) {
                    if (false !== $this->collMaterialssPartial && count($collMaterialss)) {
                      $this->initMaterialss(false);

                      foreach($collMaterialss as $obj) {
                        if (false == $this->collMaterialss->contains($obj)) {
                          $this->collMaterialss->append($obj);
                        }
                      }

                      $this->collMaterialssPartial = true;
                    }

                    $collMaterialss->getInternalIterator()->rewind();
                    return $collMaterialss;
                }

                if($partial && $this->collMaterialss) {
                    foreach($this->collMaterialss as $obj) {
                        if($obj->isNew()) {
                            $collMaterialss[] = $obj;
                        }
                    }
                }

                $this->collMaterialss = $collMaterialss;
                $this->collMaterialssPartial = false;
            }
        }

        return $this->collMaterialss;
    }

    /**
     * Sets a collection of Materials objects related by a one-to-many relationship
     * to the current object.
     * It will also schedule objects for deletion based on a diff between old objects (aka persisted)
     * and new objects from the given Propel collection.
     *
     * @param PropelCollection $materialss A Propel collection.
     * @param PropelPDO $con Optional connection object
     * @return Provinces The current object (for fluent API support)
     */
    public function setMaterialss(PropelCollection $materialss, PropelPDO $con = null)
    {
        $materialssToDelete = $this->getMaterialss(new Criteria(), $con)->diff($materialss);

        $this->materialssScheduledForDeletion = unserialize(serialize($materialssToDelete));

        foreach ($materialssToDelete as $materialsRemoved) {
            $materialsRemoved->setProvinces(null);
        }

        $this->collMaterialss = null;
        foreach ($materialss as $materials) {
            $this->addMaterials($materials);
        }

        $this->collMaterialss = $materialss;
        $this->collMaterialssPartial = false;

        return $this;
    }

    /**
     * Returns the number of related Materials objects.
     *
     * @param Criteria $criteria
     * @param boolean $distinct
     * @param PropelPDO $con
     * @return int             Count of related Materials objects.
     * @throws PropelException
     */
    public function countMaterialss(Criteria $criteria = null, $distinct = false, PropelPDO $con = null)
    {
        $partial = $this->collMaterialssPartial && !$this->isNew();
        if (null === $this->collMaterialss || null !== $criteria || $partial) {
            if ($this->isNew() && null === $this->collMaterialss) {
                return 0;
            }

            if($partial && !$criteria) {
                return count($this->getMaterialss());
            }
            $query = MaterialsQuery::create(null, $criteria);
            if ($distinct) {
                $query->distinct();
            }

            return $query
                ->filterByProvinces($this)
                ->count($con);
        }

        return count($this->collMaterialss);
    }

    /**
     * Method called to associate a Materials object to this object
     * through the Materials foreign key attribute.
     *
     * @param    Materials $l Materials
     * @return Provinces The current object (for fluent API support)
     */
    public function addMaterials(Materials $l)
    {
        if ($this->collMaterialss === null) {
            $this->initMaterialss();
            $this->collMaterialssPartial = true;
        }
        if (!in_array($l, $this->collMaterialss->getArrayCopy(), true)) { // only add it if the **same** object is not already associated
            $this->doAddMaterials($l);
        }

        return $this;
    }

    /**
     * @param	Materials $materials The materials object to add.
     */
    protected function doAddMaterials($materials)
    {
        $this->collMaterialss[]= $materials;
        $materials->setProvinces($this);
    }

    /**
     * @param	Materials $materials The materials object to remove.
     * @return Provinces The current object (for fluent API support)
     */
    public function removeMaterials($materials)
    {
        if ($this->getMaterialss()->contains($materials)) {
            $this->collMaterialss->remove($this->collMaterialss->search($materials));
            if (null === $this->materialssScheduledForDeletion) {
                $this->materialssScheduledForDeletion = clone $this->collMaterialss;
                $this->materialssScheduledForDeletion->clear();
            }
            $this->materialssScheduledForDeletion[]= clone $materials;
            $materials->setProvinces(null);
        }

        return $this;
    }


    /**
     * If this collection has already been initialized with
     * an identical criteria, it returns the collection.
     * Otherwise if this Provinces is new, it will return
     * an empty collection; or if this Provinces has previously
     * been saved, it will retrieve related Materialss from storage.
     *
     * This method is protected by default in order to keep the public
     * api reasonable.  You can provide public methods for those you
     * actually need in Provinces.
     *
     * @param Criteria $criteria optional Criteria object to narrow the query
     * @param PropelPDO $con optional connection object
     * @param string $join_behavior optional join type to use (defaults to Criteria::LEFT_JOIN)
     * @return PropelObjectCollection|Materials[] List of Materials objects
     */
    public function getMaterialssJoinMaterialDefs($criteria = null, $con = null, $join_behavior = Criteria::LEFT_JOIN)
    {
        $query = MaterialsQuery::create(null, $criteria);
        $query->joinWith('MaterialDefs', $join_behavior);

        return $this->getMaterialss($query, $con);
    }

    /**
     * Clears out the collProvinceHistoriess collection
     *
     * This does not modify the database; however, it will remove any associated objects, causing
     * them to be refetched by subsequent calls to accessor method.
     *
     * @return Provinces The current object (for fluent API support)
     * @see        addProvinceHistoriess()
     */
    public function clearProvinceHistoriess()
    {
        $this->collProvinceHistoriess = null; // important to set this to null since that means it is uninitialized
        $this->collProvinceHistoriessPartial = null;

        return $this;
    }

    /**
     * reset is the collProvinceHistoriess collection loaded partially
     *
     * @return void
     */
    public function resetPartialProvinceHistoriess($v = true)
    {
        $this->collProvinceHistoriessPartial = $v;
    }

    /**
     * Initializes the collProvinceHistoriess collection.
     *
     * By default this just sets the collProvinceHistoriess collection to an empty array (like clearcollProvinceHistoriess());
     * however, you may wish to override this method in your stub class to provide setting appropriate
     * to your application -- for example, setting the initial array to the values stored in database.
     *
     * @param boolean $overrideExisting If set to true, the method call initializes
     *                                        the collection even if it is not empty
     *
     * @return void
     */
    public function initProvinceHistoriess($overrideExisting = true)
    {
        if (null !== $this->collProvinceHistoriess && !$overrideExisting) {
            return;
        }
        $this->collProvinceHistoriess = new PropelObjectCollection();
        $this->collProvinceHistoriess->setModel('ProvinceHistories');
    }

    /**
     * Gets an array of ProvinceHistories objects which contain a foreign key that references this object.
     *
     * If the $criteria is not null, it is used to always fetch the results from the database.
     * Otherwise the results are fetched from the database the first time, then cached.
     * Next time the same method is called without $criteria, the cached collection is returned.
     * If this Provinces is new, it will return
     * an empty collection or the current collection; the criteria is ignored on a new object.
     *
     * @param Criteria $criteria optional Criteria object to narrow the query
     * @param PropelPDO $con optional connection object
     * @return PropelObjectCollection|ProvinceHistories[] List of ProvinceHistories objects
     * @throws PropelException
     */
    public function getProvinceHistoriess($criteria = null, PropelPDO $con = null)
    {
        $partial = $this->collProvinceHistoriessPartial && !$this->isNew();
        if (null === $this->collProvinceHistoriess || null !== $criteria  || $partial) {
            if ($this->isNew() && null === $this->collProvinceHistoriess) {
                // return empty collection
                $this->initProvinceHistoriess();
            } else {
                $collProvinceHistoriess = ProvinceHistoriesQuery::create(null, $criteria)
                    ->filterByProvinces($this)
                    ->find($con);
                if (null !== $criteria) {
                    if (false !== $this->collProvinceHistoriessPartial && count($collProvinceHistoriess)) {
                      $this->initProvinceHistoriess(false);

                      foreach($collProvinceHistoriess as $obj) {
                        if (false == $this->collProvinceHistoriess->contains($obj)) {
                          $this->collProvinceHistoriess->append($obj);
                        }
                      }

                      $this->collProvinceHistoriessPartial = true;
                    }

                    $collProvinceHistoriess->getInternalIterator()->rewind();
                    return $collProvinceHistoriess;
                }

                if($partial && $this->collProvinceHistoriess) {
                    foreach($this->collProvinceHistoriess as $obj) {
                        if($obj->isNew()) {
                            $collProvinceHistoriess[] = $obj;
                        }
                    }
                }

                $this->collProvinceHistoriess = $collProvinceHistoriess;
                $this->collProvinceHistoriessPartial = false;
            }
        }

        return $this->collProvinceHistoriess;
    }

    /**
     * Sets a collection of ProvinceHistories objects related by a one-to-many relationship
     * to the current object.
     * It will also schedule objects for deletion based on a diff between old objects (aka persisted)
     * and new objects from the given Propel collection.
     *
     * @param PropelCollection $provinceHistoriess A Propel collection.
     * @param PropelPDO $con Optional connection object
     * @return Provinces The current object (for fluent API support)
     */
    public function setProvinceHistoriess(PropelCollection $provinceHistoriess, PropelPDO $con = null)
    {
        $provinceHistoriessToDelete = $this->getProvinceHistoriess(new Criteria(), $con)->diff($provinceHistoriess);

        $this->provinceHistoriessScheduledForDeletion = unserialize(serialize($provinceHistoriessToDelete));

        foreach ($provinceHistoriessToDelete as $provinceHistoriesRemoved) {
            $provinceHistoriesRemoved->setProvinces(null);
        }

        $this->collProvinceHistoriess = null;
        foreach ($provinceHistoriess as $provinceHistories) {
            $this->addProvinceHistories($provinceHistories);
        }

        $this->collProvinceHistoriess = $provinceHistoriess;
        $this->collProvinceHistoriessPartial = false;

        return $this;
    }

    /**
     * Returns the number of related ProvinceHistories objects.
     *
     * @param Criteria $criteria
     * @param boolean $distinct
     * @param PropelPDO $con
     * @return int             Count of related ProvinceHistories objects.
     * @throws PropelException
     */
    public function countProvinceHistoriess(Criteria $criteria = null, $distinct = false, PropelPDO $con = null)
    {
        $partial = $this->collProvinceHistoriessPartial && !$this->isNew();
        if (null === $this->collProvinceHistoriess || null !== $criteria || $partial) {
            if ($this->isNew() && null === $this->collProvinceHistoriess) {
                return 0;
            }

            if($partial && !$criteria) {
                return count($this->getProvinceHistoriess());
            }
            $query = ProvinceHistoriesQuery::create(null, $criteria);
            if ($distinct) {
                $query->distinct();
            }

            return $query
                ->filterByProvinces($this)
                ->count($con);
        }

        return count($this->collProvinceHistoriess);
    }

    /**
     * Method called to associate a ProvinceHistories object to this object
     * through the ProvinceHistories foreign key attribute.
     *
     * @param    ProvinceHistories $l ProvinceHistories
     * @return Provinces The current object (for fluent API support)
     */
    public function addProvinceHistories(ProvinceHistories $l)
    {
        if ($this->collProvinceHistoriess === null) {
            $this->initProvinceHistoriess();
            $this->collProvinceHistoriessPartial = true;
        }
        if (!in_array($l, $this->collProvinceHistoriess->getArrayCopy(), true)) { // only add it if the **same** object is not already associated
            $this->doAddProvinceHistories($l);
        }

        return $this;
    }

    /**
     * @param	ProvinceHistories $provinceHistories The provinceHistories object to add.
     */
    protected function doAddProvinceHistories($provinceHistories)
    {
        $this->collProvinceHistoriess[]= $provinceHistories;
        $provinceHistories->setProvinces($this);
    }

    /**
     * @param	ProvinceHistories $provinceHistories The provinceHistories object to remove.
     * @return Provinces The current object (for fluent API support)
     */
    public function removeProvinceHistories($provinceHistories)
    {
        if ($this->getProvinceHistoriess()->contains($provinceHistories)) {
            $this->collProvinceHistoriess->remove($this->collProvinceHistoriess->search($provinceHistories));
            if (null === $this->provinceHistoriessScheduledForDeletion) {
                $this->provinceHistoriessScheduledForDeletion = clone $this->collProvinceHistoriess;
                $this->provinceHistoriessScheduledForDeletion->clear();
            }
            $this->provinceHistoriessScheduledForDeletion[]= clone $provinceHistories;
            $provinceHistories->setProvinces(null);
        }

        return $this;
    }


    /**
     * If this collection has already been initialized with
     * an identical criteria, it returns the collection.
     * Otherwise if this Provinces is new, it will return
     * an empty collection; or if this Provinces has previously
     * been saved, it will retrieve related ProvinceHistoriess from storage.
     *
     * This method is protected by default in order to keep the public
     * api reasonable.  You can provide public methods for those you
     * actually need in Provinces.
     *
     * @param Criteria $criteria optional Criteria object to narrow the query
     * @param PropelPDO $con optional connection object
     * @param string $join_behavior optional join type to use (defaults to Criteria::LEFT_JOIN)
     * @return PropelObjectCollection|ProvinceHistories[] List of ProvinceHistories objects
     */
    public function getProvinceHistoriessJoinPlayers($criteria = null, $con = null, $join_behavior = Criteria::LEFT_JOIN)
    {
        $query = ProvinceHistoriesQuery::create(null, $criteria);
        $query->joinWith('Players', $join_behavior);

        return $this->getProvinceHistoriess($query, $con);
    }

    /**
     * Clears out the collWarsRelatedByAttackerProvinceId collection
     *
     * This does not modify the database; however, it will remove any associated objects, causing
     * them to be refetched by subsequent calls to accessor method.
     *
     * @return Provinces The current object (for fluent API support)
     * @see        addWarsRelatedByAttackerProvinceId()
     */
    public function clearWarsRelatedByAttackerProvinceId()
    {
        $this->collWarsRelatedByAttackerProvinceId = null; // important to set this to null since that means it is uninitialized
        $this->collWarsRelatedByAttackerProvinceIdPartial = null;

        return $this;
    }

    /**
     * reset is the collWarsRelatedByAttackerProvinceId collection loaded partially
     *
     * @return void
     */
    public function resetPartialWarsRelatedByAttackerProvinceId($v = true)
    {
        $this->collWarsRelatedByAttackerProvinceIdPartial = $v;
    }

    /**
     * Initializes the collWarsRelatedByAttackerProvinceId collection.
     *
     * By default this just sets the collWarsRelatedByAttackerProvinceId collection to an empty array (like clearcollWarsRelatedByAttackerProvinceId());
     * however, you may wish to override this method in your stub class to provide setting appropriate
     * to your application -- for example, setting the initial array to the values stored in database.
     *
     * @param boolean $overrideExisting If set to true, the method call initializes
     *                                        the collection even if it is not empty
     *
     * @return void
     */
    public function initWarsRelatedByAttackerProvinceId($overrideExisting = true)
    {
        if (null !== $this->collWarsRelatedByAttackerProvinceId && !$overrideExisting) {
            return;
        }
        $this->collWarsRelatedByAttackerProvinceId = new PropelObjectCollection();
        $this->collWarsRelatedByAttackerProvinceId->setModel('War');
    }

    /**
     * Gets an array of War objects which contain a foreign key that references this object.
     *
     * If the $criteria is not null, it is used to always fetch the results from the database.
     * Otherwise the results are fetched from the database the first time, then cached.
     * Next time the same method is called without $criteria, the cached collection is returned.
     * If this Provinces is new, it will return
     * an empty collection or the current collection; the criteria is ignored on a new object.
     *
     * @param Criteria $criteria optional Criteria object to narrow the query
     * @param PropelPDO $con optional connection object
     * @return PropelObjectCollection|War[] List of War objects
     * @throws PropelException
     */
    public function getWarsRelatedByAttackerProvinceId($criteria = null, PropelPDO $con = null)
    {
        $partial = $this->collWarsRelatedByAttackerProvinceIdPartial && !$this->isNew();
        if (null === $this->collWarsRelatedByAttackerProvinceId || null !== $criteria  || $partial) {
            if ($this->isNew() && null === $this->collWarsRelatedByAttackerProvinceId) {
                // return empty collection
                $this->initWarsRelatedByAttackerProvinceId();
            } else {
                $collWarsRelatedByAttackerProvinceId = WarQuery::create(null, $criteria)
                    ->filterByProvincesRelatedByAttackerProvinceId($this)
                    ->find($con);
                if (null !== $criteria) {
                    if (false !== $this->collWarsRelatedByAttackerProvinceIdPartial && count($collWarsRelatedByAttackerProvinceId)) {
                      $this->initWarsRelatedByAttackerProvinceId(false);

                      foreach($collWarsRelatedByAttackerProvinceId as $obj) {
                        if (false == $this->collWarsRelatedByAttackerProvinceId->contains($obj)) {
                          $this->collWarsRelatedByAttackerProvinceId->append($obj);
                        }
                      }

                      $this->collWarsRelatedByAttackerProvinceIdPartial = true;
                    }

                    $collWarsRelatedByAttackerProvinceId->getInternalIterator()->rewind();
                    return $collWarsRelatedByAttackerProvinceId;
                }

                if($partial && $this->collWarsRelatedByAttackerProvinceId) {
                    foreach($this->collWarsRelatedByAttackerProvinceId as $obj) {
                        if($obj->isNew()) {
                            $collWarsRelatedByAttackerProvinceId[] = $obj;
                        }
                    }
                }

                $this->collWarsRelatedByAttackerProvinceId = $collWarsRelatedByAttackerProvinceId;
                $this->collWarsRelatedByAttackerProvinceIdPartial = false;
            }
        }

        return $this->collWarsRelatedByAttackerProvinceId;
    }

    /**
     * Sets a collection of WarRelatedByAttackerProvinceId objects related by a one-to-many relationship
     * to the current object.
     * It will also schedule objects for deletion based on a diff between old objects (aka persisted)
     * and new objects from the given Propel collection.
     *
     * @param PropelCollection $warsRelatedByAttackerProvinceId A Propel collection.
     * @param PropelPDO $con Optional connection object
     * @return Provinces The current object (for fluent API support)
     */
    public function setWarsRelatedByAttackerProvinceId(PropelCollection $warsRelatedByAttackerProvinceId, PropelPDO $con = null)
    {
        $warsRelatedByAttackerProvinceIdToDelete = $this->getWarsRelatedByAttackerProvinceId(new Criteria(), $con)->diff($warsRelatedByAttackerProvinceId);

        $this->warsRelatedByAttackerProvinceIdScheduledForDeletion = unserialize(serialize($warsRelatedByAttackerProvinceIdToDelete));

        foreach ($warsRelatedByAttackerProvinceIdToDelete as $warRelatedByAttackerProvinceIdRemoved) {
            $warRelatedByAttackerProvinceIdRemoved->setProvincesRelatedByAttackerProvinceId(null);
        }

        $this->collWarsRelatedByAttackerProvinceId = null;
        foreach ($warsRelatedByAttackerProvinceId as $warRelatedByAttackerProvinceId) {
            $this->addWarRelatedByAttackerProvinceId($warRelatedByAttackerProvinceId);
        }

        $this->collWarsRelatedByAttackerProvinceId = $warsRelatedByAttackerProvinceId;
        $this->collWarsRelatedByAttackerProvinceIdPartial = false;

        return $this;
    }

    /**
     * Returns the number of related War objects.
     *
     * @param Criteria $criteria
     * @param boolean $distinct
     * @param PropelPDO $con
     * @return int             Count of related War objects.
     * @throws PropelException
     */
    public function countWarsRelatedByAttackerProvinceId(Criteria $criteria = null, $distinct = false, PropelPDO $con = null)
    {
        $partial = $this->collWarsRelatedByAttackerProvinceIdPartial && !$this->isNew();
        if (null === $this->collWarsRelatedByAttackerProvinceId || null !== $criteria || $partial) {
            if ($this->isNew() && null === $this->collWarsRelatedByAttackerProvinceId) {
                return 0;
            }

            if($partial && !$criteria) {
                return count($this->getWarsRelatedByAttackerProvinceId());
            }
            $query = WarQuery::create(null, $criteria);
            if ($distinct) {
                $query->distinct();
            }

            return $query
                ->filterByProvincesRelatedByAttackerProvinceId($this)
                ->count($con);
        }

        return count($this->collWarsRelatedByAttackerProvinceId);
    }

    /**
     * Method called to associate a War object to this object
     * through the War foreign key attribute.
     *
     * @param    War $l War
     * @return Provinces The current object (for fluent API support)
     */
    public function addWarRelatedByAttackerProvinceId(War $l)
    {
        if ($this->collWarsRelatedByAttackerProvinceId === null) {
            $this->initWarsRelatedByAttackerProvinceId();
            $this->collWarsRelatedByAttackerProvinceIdPartial = true;
        }
        if (!in_array($l, $this->collWarsRelatedByAttackerProvinceId->getArrayCopy(), true)) { // only add it if the **same** object is not already associated
            $this->doAddWarRelatedByAttackerProvinceId($l);
        }

        return $this;
    }

    /**
     * @param	WarRelatedByAttackerProvinceId $warRelatedByAttackerProvinceId The warRelatedByAttackerProvinceId object to add.
     */
    protected function doAddWarRelatedByAttackerProvinceId($warRelatedByAttackerProvinceId)
    {
        $this->collWarsRelatedByAttackerProvinceId[]= $warRelatedByAttackerProvinceId;
        $warRelatedByAttackerProvinceId->setProvincesRelatedByAttackerProvinceId($this);
    }

    /**
     * @param	WarRelatedByAttackerProvinceId $warRelatedByAttackerProvinceId The warRelatedByAttackerProvinceId object to remove.
     * @return Provinces The current object (for fluent API support)
     */
    public function removeWarRelatedByAttackerProvinceId($warRelatedByAttackerProvinceId)
    {
        if ($this->getWarsRelatedByAttackerProvinceId()->contains($warRelatedByAttackerProvinceId)) {
            $this->collWarsRelatedByAttackerProvinceId->remove($this->collWarsRelatedByAttackerProvinceId->search($warRelatedByAttackerProvinceId));
            if (null === $this->warsRelatedByAttackerProvinceIdScheduledForDeletion) {
                $this->warsRelatedByAttackerProvinceIdScheduledForDeletion = clone $this->collWarsRelatedByAttackerProvinceId;
                $this->warsRelatedByAttackerProvinceIdScheduledForDeletion->clear();
            }
            $this->warsRelatedByAttackerProvinceIdScheduledForDeletion[]= clone $warRelatedByAttackerProvinceId;
            $warRelatedByAttackerProvinceId->setProvincesRelatedByAttackerProvinceId(null);
        }

        return $this;
    }


    /**
     * If this collection has already been initialized with
     * an identical criteria, it returns the collection.
     * Otherwise if this Provinces is new, it will return
     * an empty collection; or if this Provinces has previously
     * been saved, it will retrieve related WarsRelatedByAttackerProvinceId from storage.
     *
     * This method is protected by default in order to keep the public
     * api reasonable.  You can provide public methods for those you
     * actually need in Provinces.
     *
     * @param Criteria $criteria optional Criteria object to narrow the query
     * @param PropelPDO $con optional connection object
     * @param string $join_behavior optional join type to use (defaults to Criteria::LEFT_JOIN)
     * @return PropelObjectCollection|War[] List of War objects
     */
    public function getWarsRelatedByAttackerProvinceIdJoinRounds($criteria = null, $con = null, $join_behavior = Criteria::LEFT_JOIN)
    {
        $query = WarQuery::create(null, $criteria);
        $query->joinWith('Rounds', $join_behavior);

        return $this->getWarsRelatedByAttackerProvinceId($query, $con);
    }

    /**
     * Clears out the collWarsRelatedByOpponentProvinceId collection
     *
     * This does not modify the database; however, it will remove any associated objects, causing
     * them to be refetched by subsequent calls to accessor method.
     *
     * @return Provinces The current object (for fluent API support)
     * @see        addWarsRelatedByOpponentProvinceId()
     */
    public function clearWarsRelatedByOpponentProvinceId()
    {
        $this->collWarsRelatedByOpponentProvinceId = null; // important to set this to null since that means it is uninitialized
        $this->collWarsRelatedByOpponentProvinceIdPartial = null;

        return $this;
    }

    /**
     * reset is the collWarsRelatedByOpponentProvinceId collection loaded partially
     *
     * @return void
     */
    public function resetPartialWarsRelatedByOpponentProvinceId($v = true)
    {
        $this->collWarsRelatedByOpponentProvinceIdPartial = $v;
    }

    /**
     * Initializes the collWarsRelatedByOpponentProvinceId collection.
     *
     * By default this just sets the collWarsRelatedByOpponentProvinceId collection to an empty array (like clearcollWarsRelatedByOpponentProvinceId());
     * however, you may wish to override this method in your stub class to provide setting appropriate
     * to your application -- for example, setting the initial array to the values stored in database.
     *
     * @param boolean $overrideExisting If set to true, the method call initializes
     *                                        the collection even if it is not empty
     *
     * @return void
     */
    public function initWarsRelatedByOpponentProvinceId($overrideExisting = true)
    {
        if (null !== $this->collWarsRelatedByOpponentProvinceId && !$overrideExisting) {
            return;
        }
        $this->collWarsRelatedByOpponentProvinceId = new PropelObjectCollection();
        $this->collWarsRelatedByOpponentProvinceId->setModel('War');
    }

    /**
     * Gets an array of War objects which contain a foreign key that references this object.
     *
     * If the $criteria is not null, it is used to always fetch the results from the database.
     * Otherwise the results are fetched from the database the first time, then cached.
     * Next time the same method is called without $criteria, the cached collection is returned.
     * If this Provinces is new, it will return
     * an empty collection or the current collection; the criteria is ignored on a new object.
     *
     * @param Criteria $criteria optional Criteria object to narrow the query
     * @param PropelPDO $con optional connection object
     * @return PropelObjectCollection|War[] List of War objects
     * @throws PropelException
     */
    public function getWarsRelatedByOpponentProvinceId($criteria = null, PropelPDO $con = null)
    {
        $partial = $this->collWarsRelatedByOpponentProvinceIdPartial && !$this->isNew();
        if (null === $this->collWarsRelatedByOpponentProvinceId || null !== $criteria  || $partial) {
            if ($this->isNew() && null === $this->collWarsRelatedByOpponentProvinceId) {
                // return empty collection
                $this->initWarsRelatedByOpponentProvinceId();
            } else {
                $collWarsRelatedByOpponentProvinceId = WarQuery::create(null, $criteria)
                    ->filterByProvincesRelatedByOpponentProvinceId($this)
                    ->find($con);
                if (null !== $criteria) {
                    if (false !== $this->collWarsRelatedByOpponentProvinceIdPartial && count($collWarsRelatedByOpponentProvinceId)) {
                      $this->initWarsRelatedByOpponentProvinceId(false);

                      foreach($collWarsRelatedByOpponentProvinceId as $obj) {
                        if (false == $this->collWarsRelatedByOpponentProvinceId->contains($obj)) {
                          $this->collWarsRelatedByOpponentProvinceId->append($obj);
                        }
                      }

                      $this->collWarsRelatedByOpponentProvinceIdPartial = true;
                    }

                    $collWarsRelatedByOpponentProvinceId->getInternalIterator()->rewind();
                    return $collWarsRelatedByOpponentProvinceId;
                }

                if($partial && $this->collWarsRelatedByOpponentProvinceId) {
                    foreach($this->collWarsRelatedByOpponentProvinceId as $obj) {
                        if($obj->isNew()) {
                            $collWarsRelatedByOpponentProvinceId[] = $obj;
                        }
                    }
                }

                $this->collWarsRelatedByOpponentProvinceId = $collWarsRelatedByOpponentProvinceId;
                $this->collWarsRelatedByOpponentProvinceIdPartial = false;
            }
        }

        return $this->collWarsRelatedByOpponentProvinceId;
    }

    /**
     * Sets a collection of WarRelatedByOpponentProvinceId objects related by a one-to-many relationship
     * to the current object.
     * It will also schedule objects for deletion based on a diff between old objects (aka persisted)
     * and new objects from the given Propel collection.
     *
     * @param PropelCollection $warsRelatedByOpponentProvinceId A Propel collection.
     * @param PropelPDO $con Optional connection object
     * @return Provinces The current object (for fluent API support)
     */
    public function setWarsRelatedByOpponentProvinceId(PropelCollection $warsRelatedByOpponentProvinceId, PropelPDO $con = null)
    {
        $warsRelatedByOpponentProvinceIdToDelete = $this->getWarsRelatedByOpponentProvinceId(new Criteria(), $con)->diff($warsRelatedByOpponentProvinceId);

        $this->warsRelatedByOpponentProvinceIdScheduledForDeletion = unserialize(serialize($warsRelatedByOpponentProvinceIdToDelete));

        foreach ($warsRelatedByOpponentProvinceIdToDelete as $warRelatedByOpponentProvinceIdRemoved) {
            $warRelatedByOpponentProvinceIdRemoved->setProvincesRelatedByOpponentProvinceId(null);
        }

        $this->collWarsRelatedByOpponentProvinceId = null;
        foreach ($warsRelatedByOpponentProvinceId as $warRelatedByOpponentProvinceId) {
            $this->addWarRelatedByOpponentProvinceId($warRelatedByOpponentProvinceId);
        }

        $this->collWarsRelatedByOpponentProvinceId = $warsRelatedByOpponentProvinceId;
        $this->collWarsRelatedByOpponentProvinceIdPartial = false;

        return $this;
    }

    /**
     * Returns the number of related War objects.
     *
     * @param Criteria $criteria
     * @param boolean $distinct
     * @param PropelPDO $con
     * @return int             Count of related War objects.
     * @throws PropelException
     */
    public function countWarsRelatedByOpponentProvinceId(Criteria $criteria = null, $distinct = false, PropelPDO $con = null)
    {
        $partial = $this->collWarsRelatedByOpponentProvinceIdPartial && !$this->isNew();
        if (null === $this->collWarsRelatedByOpponentProvinceId || null !== $criteria || $partial) {
            if ($this->isNew() && null === $this->collWarsRelatedByOpponentProvinceId) {
                return 0;
            }

            if($partial && !$criteria) {
                return count($this->getWarsRelatedByOpponentProvinceId());
            }
            $query = WarQuery::create(null, $criteria);
            if ($distinct) {
                $query->distinct();
            }

            return $query
                ->filterByProvincesRelatedByOpponentProvinceId($this)
                ->count($con);
        }

        return count($this->collWarsRelatedByOpponentProvinceId);
    }

    /**
     * Method called to associate a War object to this object
     * through the War foreign key attribute.
     *
     * @param    War $l War
     * @return Provinces The current object (for fluent API support)
     */
    public function addWarRelatedByOpponentProvinceId(War $l)
    {
        if ($this->collWarsRelatedByOpponentProvinceId === null) {
            $this->initWarsRelatedByOpponentProvinceId();
            $this->collWarsRelatedByOpponentProvinceIdPartial = true;
        }
        if (!in_array($l, $this->collWarsRelatedByOpponentProvinceId->getArrayCopy(), true)) { // only add it if the **same** object is not already associated
            $this->doAddWarRelatedByOpponentProvinceId($l);
        }

        return $this;
    }

    /**
     * @param	WarRelatedByOpponentProvinceId $warRelatedByOpponentProvinceId The warRelatedByOpponentProvinceId object to add.
     */
    protected function doAddWarRelatedByOpponentProvinceId($warRelatedByOpponentProvinceId)
    {
        $this->collWarsRelatedByOpponentProvinceId[]= $warRelatedByOpponentProvinceId;
        $warRelatedByOpponentProvinceId->setProvincesRelatedByOpponentProvinceId($this);
    }

    /**
     * @param	WarRelatedByOpponentProvinceId $warRelatedByOpponentProvinceId The warRelatedByOpponentProvinceId object to remove.
     * @return Provinces The current object (for fluent API support)
     */
    public function removeWarRelatedByOpponentProvinceId($warRelatedByOpponentProvinceId)
    {
        if ($this->getWarsRelatedByOpponentProvinceId()->contains($warRelatedByOpponentProvinceId)) {
            $this->collWarsRelatedByOpponentProvinceId->remove($this->collWarsRelatedByOpponentProvinceId->search($warRelatedByOpponentProvinceId));
            if (null === $this->warsRelatedByOpponentProvinceIdScheduledForDeletion) {
                $this->warsRelatedByOpponentProvinceIdScheduledForDeletion = clone $this->collWarsRelatedByOpponentProvinceId;
                $this->warsRelatedByOpponentProvinceIdScheduledForDeletion->clear();
            }
            $this->warsRelatedByOpponentProvinceIdScheduledForDeletion[]= clone $warRelatedByOpponentProvinceId;
            $warRelatedByOpponentProvinceId->setProvincesRelatedByOpponentProvinceId(null);
        }

        return $this;
    }


    /**
     * If this collection has already been initialized with
     * an identical criteria, it returns the collection.
     * Otherwise if this Provinces is new, it will return
     * an empty collection; or if this Provinces has previously
     * been saved, it will retrieve related WarsRelatedByOpponentProvinceId from storage.
     *
     * This method is protected by default in order to keep the public
     * api reasonable.  You can provide public methods for those you
     * actually need in Provinces.
     *
     * @param Criteria $criteria optional Criteria object to narrow the query
     * @param PropelPDO $con optional connection object
     * @param string $join_behavior optional join type to use (defaults to Criteria::LEFT_JOIN)
     * @return PropelObjectCollection|War[] List of War objects
     */
    public function getWarsRelatedByOpponentProvinceIdJoinRounds($criteria = null, $con = null, $join_behavior = Criteria::LEFT_JOIN)
    {
        $query = WarQuery::create(null, $criteria);
        $query->joinWith('Rounds', $join_behavior);

        return $this->getWarsRelatedByOpponentProvinceId($query, $con);
    }

    /**
     * Clears the current object and sets all attributes to their default values
     */
    public function clear()
    {
        $this->id = null;
        $this->game_id = null;
        $this->player_id = null;
        $this->province_def_id = null;
        $this->active = null;
        $this->alreadyInSave = false;
        $this->alreadyInValidation = false;
        $this->alreadyInClearAllReferencesDeep = false;
        $this->clearAllReferences();
        $this->applyDefaultValues();
        $this->resetModified();
        $this->setNew(true);
        $this->setDeleted(false);
    }

    /**
     * Resets all references to other model objects or collections of model objects.
     *
     * This method is a user-space workaround for PHP's inability to garbage collect
     * objects with circular references (even in PHP 5.3). This is currently necessary
     * when using Propel in certain daemon or large-volumne/high-memory operations.
     *
     * @param boolean $deep Whether to also clear the references on all referrer objects.
     */
    public function clearAllReferences($deep = false)
    {
        if ($deep && !$this->alreadyInClearAllReferencesDeep) {
            $this->alreadyInClearAllReferencesDeep = true;
            if ($this->collArmys) {
                foreach ($this->collArmys as $o) {
                    $o->clearAllReferences($deep);
                }
            }
            if ($this->collInfrastructuress) {
                foreach ($this->collInfrastructuress as $o) {
                    $o->clearAllReferences($deep);
                }
            }
            if ($this->collMaterialss) {
                foreach ($this->collMaterialss as $o) {
                    $o->clearAllReferences($deep);
                }
            }
            if ($this->collProvinceHistoriess) {
                foreach ($this->collProvinceHistoriess as $o) {
                    $o->clearAllReferences($deep);
                }
            }
            if ($this->collWarsRelatedByAttackerProvinceId) {
                foreach ($this->collWarsRelatedByAttackerProvinceId as $o) {
                    $o->clearAllReferences($deep);
                }
            }
            if ($this->collWarsRelatedByOpponentProvinceId) {
                foreach ($this->collWarsRelatedByOpponentProvinceId as $o) {
                    $o->clearAllReferences($deep);
                }
            }
            if ($this->aProvinceDefs instanceof Persistent) {
              $this->aProvinceDefs->clearAllReferences($deep);
            }
            if ($this->aPlayers instanceof Persistent) {
              $this->aPlayers->clearAllReferences($deep);
            }

            $this->alreadyInClearAllReferencesDeep = false;
        } // if ($deep)

        if ($this->collArmys instanceof PropelCollection) {
            $this->collArmys->clearIterator();
        }
        $this->collArmys = null;
        if ($this->collInfrastructuress instanceof PropelCollection) {
            $this->collInfrastructuress->clearIterator();
        }
        $this->collInfrastructuress = null;
        if ($this->collMaterialss instanceof PropelCollection) {
            $this->collMaterialss->clearIterator();
        }
        $this->collMaterialss = null;
        if ($this->collProvinceHistoriess instanceof PropelCollection) {
            $this->collProvinceHistoriess->clearIterator();
        }
        $this->collProvinceHistoriess = null;
        if ($this->collWarsRelatedByAttackerProvinceId instanceof PropelCollection) {
            $this->collWarsRelatedByAttackerProvinceId->clearIterator();
        }
        $this->collWarsRelatedByAttackerProvinceId = null;
        if ($this->collWarsRelatedByOpponentProvinceId instanceof PropelCollection) {
            $this->collWarsRelatedByOpponentProvinceId->clearIterator();
        }
        $this->collWarsRelatedByOpponentProvinceId = null;
        $this->aProvinceDefs = null;
        $this->aPlayers = null;
    }

    /**
     * return the string representation of this object
     *
     * @return string
     */
    public function __toString()
    {
        return (string) $this->exportTo(ProvincesPeer::DEFAULT_STRING_FORMAT);
    }

    /**
     * return true is the object is in saving state
     *
     * @return boolean
     */
    public function isAlreadyInSave()
    {
        return $this->alreadyInSave;
    }

}
